package com.sinbad.interview.solution.util;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

/**
 * 用于生成一个大小约为 200MB 的 .txt 文件，文件中包含大量随机数字，每行一个数字
 *
 * @author sinbad.cheng
 * @since 2025-04-14 16:40
 */
public class NumberFileGenerator {

    public static void main(String[] args) {
        String filePath = "numbers.txt"; // 输出文件路径
        long targetSize = 200L * 1024 * 1024; // 200MB = 209_715_200 字节
        long currentSize = 0;

        Random random = new Random();

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            while (currentSize < targetSize) {
                int number = random.nextInt(1_000_000) + 1; // 1 到 1000000 的随机数
                String line = number + "\n";
                writer.write(line);
                currentSize += line.getBytes().length; // 使用 UTF-8 编码估算字节长度
            }
            System.out.println("生成完成，文件路径：" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
